package com.zmj.controller;

import com.zmj.entity.UserInfo;
import com.zmj.service.UserInfoService;
import com.zmj.utils.Message;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Api(tags = {"用户列表"})
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;

    @RequestMapping("/toMain")
    @ApiOperation(value = "查询用户",notes = "根据用户的状态查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 400,message = "客户端请求参数填写异常"),
            @ApiResponse(code = 404,message = "您所请求的资源无法找到")
    })
    public String selInfos(Map<String,Object> map){
        List<UserInfo> userInfoList= userInfoService.selInfos(1);
        map.put("userInfoList",userInfoList);
        return "main";
    }

    @ApiOperation(value = "删除用户",notes = "更改用户的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true)

    })
    @ApiResponses({
            @ApiResponse(code = 400,message = "客户端请求参数填写异常"),
            @ApiResponse(code = 404,message = "您所请求的资源无法找到")
    })
    @RequestMapping("/delInfo")
    @ResponseBody
    public Message delInfo(Integer id){
       boolean flg= userInfoService.delInfo(0,id);
        Message message=new Message();
        if (flg){
            message.setCode(1);
            message.setMessage("删除成功");
        } else {
            message.setCode(0);
            message.setMessage("删除失败");
        }
        return message;
    }

    @ApiOperation(value = "添加或修改用户信息",notes = "此方法是修改以及添加的共同方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfo", value = "用户信息详情", required = true)

    })
    @ApiResponses({
            @ApiResponse(code = 400,message = "客户端请求参数填写异常"),
            @ApiResponse(code = 404,message = "您所请求的资源无法找到")
    })
    @RequestMapping("/addAndUpdInfo")
    @ResponseBody
    public Message addAndUpdInfo(UserInfo userInfo) {
        int id = userInfo.getId();
        Message message = new Message();
        if (id == 0) {
            boolean flg = userInfoService.addInfo(userInfo);
            if (flg) {
                message.setCode(1);
                message.setMessage("添加成功");
            } else {
                message.setCode(0);
                message.setMessage("添加失败");
            }
            return message;
        } else {
            boolean flg = userInfoService.updateInfo(userInfo);
            if (flg) {
                message.setCode(1);
                message.setMessage("更改成功");
            } else {
                message.setCode(0);
                message.setMessage("更改失败");
            }
            return message;
        }

    }

    @ApiOperation(value = "查找用户信息",notes = "此方法是修改用户信息时回显页面用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户Id", required = true)

    })
    @ApiResponses({
            @ApiResponse(code = 400,message = "客户端请求参数填写异常"),
            @ApiResponse(code = 404,message = "您所请求的资源无法找到")
    })
    @RequestMapping("/selOneInfo")
    public String selOneInfo(Integer id,Map<String,Object> map){
        UserInfo userInfo= userInfoService.selOneInfo(1,id);
        map.put("userInfo",userInfo);
        return "edit";
    }

    @ApiOperation(value = "跳转编辑页面",notes = "此方法是跳转编辑页面")
    @ApiResponses({
            @ApiResponse(code = 400,message = "客户端请求参数填写异常"),
            @ApiResponse(code = 404,message = "您所请求的资源无法找到")
    })
    @RequestMapping("/addInfo")
    public String addInfo(Map<String,Object> map){
        UserInfo userInfo= new UserInfo();
        map.put("userInfo",userInfo);
        return "edit";
    }
}
